import _ from 'underscore';
import { fromJS } from 'immutable';

export const nullFilterValue = fromJS([-1]);

export const oneDayValueOf = 86400000;

export const anythingEvent = fromJS({
  label: '任意事件',
  value: '$Anything'
});

export const totalDimension = fromJS({
  group: '',
  options: [
    {
      label: '总体',
      value: '-1'
    }
  ]
});

export const defaultEventIndicatorItems = fromJS({
  group: '',
  options: [
    {
      label: '总次数',
      value: 'general',
      object: {
        unit: '次'
      }
    },
    {
      label: '触发用户数',
      value: 'unique',
      object: {
        unit: '人'
      }
    },
    {
      label: '人均次数',
      value: 'average',
      object: {
        unit: '次'
      }
    }
  ]
});

export const defaultEventIndicator = fromJS({
  event: anythingEvent.get('value'),
  indicators: [
    {
      value: defaultEventIndicatorItems.getIn(['options', 0, 'value']),
      getDisplayText: null,
      subValue: null
    }
  ]
});

export const defaultViewDimension = fromJS({
  property: totalDimension.getIn(['options', 0, 'value'])
});

export const defaultViewDimensionNumberSettins = fromJS({
  segmentType: 1, // 离散数字 = 0, 默认区间 = 1, 自定义区间 = 2
  values: [] // 离散数字 = [null]， 默认区间 = [], 自定义区间 = [1, 2, 3]
});

let getDisplayText = function(prefix) {
  return `${ prefix }的${ this.label }`;
};

export const defaultOptionExtensions = [
  {
    label: '总和',
    value: 'SUM'
  },
  {
    label: '均值',
    value: 'AVG'
  },
  {
    label: '最大值',
    value: 'MAX'
  },
  {
    label: '最小值',
    value: 'MIN'
  },
  {
    label: '人均值',
    value: 'uniqAvg'
  }
];
_.each(defaultOptionExtensions, p => p.getDisplayText = getDisplayText.bind(p));

export const defaultChartColors = ['#56bc76','#eac85e','#32adfa','#c596fa','#70e0e0','#a1e55c','#f2a38d','#ffe9a6','#7aa9ff','#e5603b'];
export const defaultComparedChartColors = ['#8ED2A3','#F3DEA0','#7ECBFC','#ECDDFD','#B0EEEE','#C7EF9F','#FADAD1','#FFFCF5','#C7DBFF','#EE957C'];

export let unparams = function (queryString) {
    var hash = (queryString + '').indexOf('#');
    if (hash !== -1) {
        queryString = queryString.substr(hash + 1)
    }
    var result = {};
    if (! queryString) {
        return result;
    }
    var getValue = function (source, parameter, value) {
        if (!parameter) {
            return source;
        }
        var q = /^\[([a-zA-Z-_\d$.]*)\]/;
        var l = q.exec(parameter);
        var i = l[1];
        if (i) {
            if ($.isNumeric(i)) {
                source = source || [];
            } else {
                source = source || {};
            } if (l[0] === parameter) {
                source[i] = value;
            } else {
                source[i] = getValue(source[i], parameter.substr(l[0].length), value);
            }
        } else {
            if (!$.isArray(source)) {
                source = [];
            }
            source.push(value);
        }
        return source;
    };
    var parameters = queryString.split('&');
    var regexp = /([a-zA-Z-_\d$.]*)\[([a-zA-Z-_\d$.]*)\]/;

    for (var i = 0, e = parameters.length; i < e; i++) {
        var values = parameters[i].split('=');
        var n = decodeURIComponent((values[0] || '').replace(/\+/g, '%20'));
        var k = decodeURIComponent((values[1] || '').replace(/\+/g, '%20'));
        var array = regexp.exec(n);
        if ($.isArray(array)) {
            var m = array[1];
            result[m] = getValue(result[m], n.substr(m.length), k);
        } else {
            result[n] = k;
        }
    }
    return result;
};

export let formatByValue = function (dataType, byValue) {
  let nullName = '未知';
  if (byValue === null) {
    return nullName;
  }

  byValue = byValue.toString();
  if (dataType === 'number') {
    let pair = byValue.split('~');
    if (pair[0] === '-INF') {
      return pair[1] + ' 以下';
    }
    if (pair[1] === 'INF') {
      return pair[0] + ' 以上';
    }
    return byValue || nullName;
  } else if (dataType === 'string') {
    return byValue || nullName;
  } else if (dataType === 'list') {
    return byValue.join(',');
  } else if (dataType === 'bool') {
    return byValue === '1' ? '是' : '否';
  }
};
